package cn.xorange.commons.datasource.constants;

import cn.xorange.commons.utils.lang.ObjectUtils;

/**
 * @author yangjian
 */
public class DriverType {
    private DriverType(){}

    public static final String MYSQL = "MYSQL";
    public static final String POSTGRESQL = "POSTGRESQL";
    public static final String SQLSERVER = "SQLSERVER";
    public static final String ORACLE = "ORACLE";
    public static final String SQLITE = "SQLITE";

    private static final String MYSQL_DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String POSTGRESQL_DRIVER ="org.postgresql.Driver";
    private static final String SQLSERVER_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private static final String ORACLE_DRIVER = "oracle.jdbc.driver.OracleDriver";
    private static final String SQLITE_DRIVER = "org.sqlite.JDBC";

    public static String getDriverClass(String url){
        String driver = null;
        if(url.contains("mysql")){
            driver = DriverType.MYSQL_DRIVER;
        }else if (url.contains("postgresql")){
            driver = DriverType.POSTGRESQL_DRIVER;}
        else if (url.contains("sqlserver") ){
            driver = DriverType.SQLSERVER_DRIVER;
        }else if (url.contains("oracle")){
            driver = DriverType.ORACLE_DRIVER;
        }else if (url.contains("sqlite")){
            driver = DriverType.SQLITE_DRIVER;
        }
        return driver;
    }
    public static String getDriverType(Object driverClassName){
        String dsName = ObjectUtils.toString(driverClassName);
        String type = null;
        if(dsName.contains("mysql")){
            type = DriverType.MYSQL;
        }else if (dsName.contains("postgresql") ){
            type = DriverType.POSTGRESQL;}
        else if (dsName.contains("sqlserver")){
            type = DriverType.SQLSERVER;
        }else if (dsName.contains("oracle")){
            type = DriverType.ORACLE;
        }else if (dsName.contains("sqlite")){
            type = DriverType.SQLITE;
        }
        return type;
    }

    public static String getInitSql(String dbType){
        String sql = "SELECT 1";
        if (DriverType.ORACLE.equals(dbType)){
            sql = "SELECT 1 FROM DUAL";
        }else if (DriverType.POSTGRESQL.equals(dbType)){
            sql = "select version()";
        }
        return sql;
    }
}
